package com.topnav.mc.admin.dao.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.topnav.mc.admin.entity.DeviceOnlineTotal;
import com.topnav.mc.admin.entity.bean.DeviceOnlineTotalBean;
import com.topnav.mc.admin.entity.param.DeviceOnlineTotalParam;
import com.topnav.mc.base.mc.McMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @Author: kenny
 * @Date: 2023/10/31 15:26
 */
@Mapper
public interface DeviceOnlineTotalMapper extends McMapper<DeviceOnlineTotal, DeviceOnlineTotalBean, DeviceOnlineTotalParam> {
    String commSql = " SELECT dot.*," +
            " d.name AS deviceName, o.name AS orgName, o.short_name AS shortName, o.path," +
            " u.code AS userCode, u.name AS userName" +
            " FROM mc_device_online_total dot " +
            " LEFT JOIN mc_device d ON dot.device_id = d.id " +
            " LEFT JOIN mc_organization o ON d.org_id = o.id " +
            " LEFT JOIN mc_user u ON d.user_id = u.id " +
            " ${ew.customSqlSegment}";

    @Select(commSql)
    DeviceOnlineTotalBean selectInfoOne(@Param(Constants.WRAPPER) Wrapper queryWrapper);

    @Select(commSql)
    List<DeviceOnlineTotalBean> selectInfoList(@Param(Constants.WRAPPER) Wrapper queryWrapper);

    @Select(commSql)
    Page<DeviceOnlineTotalBean> selectInfoPage(Page<DeviceOnlineTotalParam> page, @Param(Constants.WRAPPER) Wrapper queryWrapper);

    //统计在线总时长
    @Select("SELECT device_id AS deviceId,SUM(online_sec) AS onlineSec,Count(1) AS onLineCount\n" +
            "FROM mc_device_online_total dot\n" +
            "LEFT JOIN mc_device d ON dot.device_id = d.id\n" +
            "LEFT JOIN mc_organization o ON dot.device_id = o.id\n" +
            "${ew.customSqlSegment}\n" +
            "GROUP BY dot.device_id\n"

        )
    Page<DeviceOnlineTotalBean> selectPage0(Page<DeviceOnlineTotalParam> page, @Param(Constants.WRAPPER) Wrapper queryWrapper);

}
